package com.eastfair.home.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.constant.BusinessSmsMsgTemplate;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.context.StaticContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.service.HomeArrangementManageService;
import com.eastfair.home.service.HomeMsgService;
import com.eastfair.messagecenter.dto.SmsSendDTO;
import com.eastfair.messagecenter.enumeration.ChannelType;
import com.eastfair.messagecenter.model.SmsContentModel;
import com.eastfair.venuebooking.api.ArrangementManageFeign;
import com.eastfair.venueservice.dto.ArrangementManageDTO;
import com.eastfair.venueservice.dto.ArrangementManagePageQuery;
import com.eastfair.venueservice.vo.ArrangementManageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 布撤展管理 service.
 *
 * @Author lyw
 * @Date 2022 /9/13 20:25
 */
@Slf4j
@Service
public class HomeArrangementManageServiceImpl implements HomeArrangementManageService {

    @Resource
    private ArrangementManageFeign arrangementManageFeign;

    @Resource
    private HomeMsgService homeMsgService;

    @Resource(name = "threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public Page<ArrangementManageVO> queryArrangementManageList(PageParams<ArrangementManageDTO> params) {
        log.info("queryArrangementManageList - 查询布撤展管理列表分页, params={}", params);
        R<Page<ArrangementManageVO>> r = arrangementManageFeign.queryForHome(params);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    public ArrangementManageVO getArrangementManage(ArrangementManageDTO arrangementManageDTO) {
        log.info("getArrangementManage - 查询布撤展管理详情, arrangementManageDTO={}", arrangementManageDTO);
        R<ArrangementManageVO> r = arrangementManageFeign.getArrangementManage(arrangementManageDTO);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delArrangementManage(Long id) {
        log.info("delArrangementManage - 删除布撤展, id={}", id);
        R<Boolean> r = arrangementManageFeign.delArrangementManage(id);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r.getData();
    }

    @Override
    public boolean sendArrangementSmsMsg(Long id) {
        log.info("sendArrangementSmsMsg - 发送布撤展管理提醒短信, id={}", id);
        ArrangementManageDTO arrangementManageDTO = new ArrangementManageDTO();
        arrangementManageDTO.setId(id);
        ArrangementManageVO arrangementManage = getArrangementManage(arrangementManageDTO);
        if (arrangementManage == null) {
            log.error("布撤展管理记录不存在，无法发送");
            throw BizException.wrap(1001, "布撤展管理记录不存在，无法发送");
        }
        String content = String.format(BusinessSmsMsgTemplate.ARRANGEMENT_REMIND_TEMPLATE, arrangementManage.getExhibitionManageName(), arrangementManage.getSpaceName(), arrangementManage.getSiteName());
        if (StrUtil.isBlank(content)) {
            log.error("发送内容为空，无法发送");
            return false;
        }
        SmsSendDTO smsSendDTO = new SmsSendDTO();
        smsSendDTO.setBusinessType(BusinessConstant.BUSINESS_COMMON);
        smsSendDTO.setChannelType(ChannelType.SMS);
        smsSendDTO.setBusinessId(ContextUtil.getTrace());
        smsSendDTO.setSystemScene(StaticContextUtil.getProjectSystemScene());
        // 封装内容
        Map<String, Object> contentMap = new HashMap<>(16);
        contentMap.put("content", content);
        SmsContentModel smsContentModel = new SmsContentModel();
        smsContentModel.setContentMap(contentMap);
        smsSendDTO.setSmsContentModelList(Collections.singletonList(smsContentModel));
        // 发送用户的手机号
        Set<String> mobileSet = new HashSet<>();
        mobileSet.add(arrangementManage.getPrincipalPhone());
        smsSendDTO.setMobileList(mobileSet);
        // 发送时间
        smsSendDTO.setIsSendNow(BusinessConstant.YES);
        // 发送短信
        homeMsgService.sendSmsMsg(smsSendDTO);
        return true;
    }

    @Override
    public void asyncSendArrangementSmsMsg(Long id) {
        threadPoolTaskExecutor.execute(() -> sendArrangementSmsMsg(id));
    }
}
